package com.adderis.soft.domain.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.adderis.soft.domain.dao.ITypeDao;
import com.adderis.soft.domain.model.Type;

@Repository("typeDao")
public class TypeDaoImpl implements ITypeDao {

	private static final Log log = LogFactory.getLog(TypeDaoImpl.class);

	Calendar calendar = Calendar.getInstance();
	java.util.Date now = calendar.getTime();
	Timestamp nowtimesstamp = new java.sql.Timestamp(now.getTime());;

	@PersistenceContext
	private EntityManager entityManager;

	/**
	 * {@inheritDoc}
	 */
	public void persist(Type transientType) {
		log.debug("Persisting NomCommerial instance");
		try {
			transientType.setCreatetimestamp(nowtimesstamp);
			transientType.setStatutcode("A");
			entityManager.persist(transientType);

			log.debug("persist successful");

		} catch (RuntimeException re) {
			log.error("persist failed", re);
		}

	}

	/**
	 * {@inheritDoc}
	 */
	public void remove(Type persistentType) {
		log.debug("removing Type instance");
		try {
			persistentType.setStatutcode("D");
			persistentType.setDeletiontimesstamp(nowtimesstamp);
			entityManager.merge(persistentType);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.debug("remove failed", re);
		}

	}

	/**
	 * {@inheritDoc}
	 */
	public void remove(Integer TypeId) {
		this.remove(this.findById(TypeId));

	}

	/**
	 * {@inheritDoc}
	 */
	public Type merge(Type detachedType) {
		log.debug("merging Type instance");
		try {
			detachedType.setUpdatetimesstamp(nowtimesstamp);
			Type result = entityManager.merge(detachedType);
			entityManager.merge(detachedType);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public Type findById(Integer id) {
		log.debug("getting Type instance with id: " + id);
		try {
			Type instance = entityManager.find(Type.class, id);
			log.debug("findById successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("findById failed", re);
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 */

	@SuppressWarnings("unchecked")
	public List<Type> findAll() {
		log.debug("getting all Type instance");
		try {
			Query query = entityManager.createQuery("SELECT c FROM Type c");
			List<Type> typeList = (List<Type>) query.getResultList();
			log.debug("findAll successful");
			return typeList;

		} catch (RuntimeException re) {
			log.error("findAll failed", re);
			return new ArrayList<Type>();
		}

	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public List<Type> search(String searchString) {
		log.debug("Search Type instances with search string: " + searchString);
		try {
			Query query = entityManager.createQuery("select nc from Type nc where nc.Type like :searchString");
			query.setParameter("searchString", searchString);
			List<Type> typeList = (List<Type>) query.getResultList();
			log.debug("search successful");
			return typeList;
		} catch (RuntimeException re) {
			log.error("search failed", re);
			return new ArrayList<Type>();
		}
	}

}
